'''
Created on Oct 29, 2012

@author: Zhao, Qi
'''
from common import utils
from datetime import datetime
from server_meta import VoucherStatus

def voucher_response_to_db(json_data, user_id):
    receivers = json_data.get('receivers')
    db_format_array = []
    for receiver in receivers:
        db_format_array.append({'voucher_title': json_data.get('voucher_title'),
                'voucher_type': json_data.get('voucher_type'),
                'owner_id': user_id,
                'receiver_id': None,
                'receiver_phone': receiver.get('phone_number'),
                'receiver_email': receiver.get('email'),
                'receiver_fb_id': receiver.get('phone_fb_id'),
                'create_date': datetime.utcnow(),
                'expire_date': utils.date_format(json_data.get('expire_date')),
                'start_date': utils.date_format(json_data.get('start_date')),
                'update_time': datetime.utcnow(),
                'voucher_status': VoucherStatus.OPEN,
                'voucher_amount': int(json_data.get('voucher_amount') * 100),
                'voucher_remain': int(json_data.get('voucher_amount') * 100),
                'loc_left_top_longitude': json_data['location'].get('loc_left_top_longitude') if json_data.get('location')!= None else None,
                'loc_left_top_latitude': json_data['location'].get('loc_left_top_latitude') if json_data.get('location')!= None else None,
                'loc_right_bottom_longitude': json_data['location'].get('loc_right_bottom_longitude') if json_data.get('location')!= None else None,
                'loc_rigth_bottom_latitude': json_data['location'].get('loc_rigth_bottom_latitude') if json_data.get('location')!= None else None,
                'loc_display_name': json_data['location'].get('loc_display_name') if json_data.get('location')!= None else None,
                'consume_type': json_data.get('consume_type'),
                'date_type': json_data.get('date_type')
                })

    return db_format_array


def voucher_modify_to_db(json_data):
    db_format =  {'voucher_title': json_data.get('voucher_title'),
                'voucher_type': json_data.get('voucher_type'),
                'receiver_id': json_data.get('receiver_id'),
                'expire_date': utils.date_format(json_data.get('expire_date')),
                'start_date': utils.date_format(json_data.get('start_date')),
                'voucher_amount': int(json_data.get('voucher_amount') * 100),
                'loc_left_top_longitude': json_data['location'].get('loc_left_top_longitude') if json_data.get('location')!= None else None,
                'loc_left_top_latitude': json_data['location'].get('loc_left_top_latitude') if json_data.get('location')!= None else None,
                'loc_right_bottom_longitude': json_data['location'].get('loc_right_bottom_longitude') if json_data.get('location')!= None else None,
                'loc_rigth_bottom_latitude': json_data['location'].get('loc_rigth_bottom_latitude') if json_data.get('location')!= None else None,
                'loc_display_name': json_data['location'].get('loc_display_name') if json_data.get('location')!= None else None,
                'consume_type': json_data.get('consume_type'),
                'date_type': json_data.get('date_type')
                }

    return db_format

def voucher_db_to_response(db_voucher, db_user):
    #location information
    db_voucher['location'] = {}
    db_voucher['location']['loc_left_top_longitude'] = db_voucher.get('loc_left_top_longitude')
    db_voucher['location']['loc_left_top_latitude'] = db_voucher.get('loc_left_top_latitude')
    db_voucher['location']['loc_right_bottom_longitude'] = db_voucher.get('loc_right_bottom_longitude')
    db_voucher['location']['loc_rigth_bottom_latitude'] = db_voucher.get('loc_rigth_bottom_latitude')
    db_voucher.pop('loc_left_top_longitude')
    db_voucher.pop('loc_left_top_latitude')
    db_voucher.pop('loc_right_bottom_longitude')
    db_voucher.pop('loc_rigth_bottom_latitude')
    
    #amount and remain
    db_voucher['voucher_amount'] = float(db_voucher['voucher_amount']) / 100
    db_voucher['voucher_remain'] = float(db_voucher['voucher_remain']) / 100
    return db_voucher
    